package liming.tool.lock;

import java.util.concurrent.atomic.AtomicLong;

public final class LockAtomicLong {
    private final CodeLock codeLock;
    private final AtomicLong value;
    public LockAtomicLong(CodeLock codeLock) {
        this.codeLock = codeLock;
        value = new AtomicLong(0);
    }
    public long get() {
        return codeLock.lock(value::get);
    }
    public LockAtomicLong set(Number value){
        codeLock.lock(()-> this.value.set(value.longValue()));
        return this;
    }
    public long setAndGet(Number value) {
        return codeLock.lock(()-> {
            this.value.set(value.longValue());
            return get();
        });
    }
    public long getAndSet(Number value) {
        return codeLock.lock(()-> this.value.getAndSet(value.longValue()));
    }
    public long add(Number value) {
        return codeLock.lock(()-> this.value.addAndGet(value.longValue()));
    }
    public long addAndGet(Number value) {
        return codeLock.lock(()-> this.value.addAndGet(value.longValue()));
    }
    public long getAndAdd(Number value) {
        return codeLock.lock(()-> this.value.getAndAdd(value.longValue()));
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }
}
